package org.jypj.dev.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.jypj.dev.dao.ShoesDao;
import org.jypj.dev.dao.ShoesImgDao;
import org.jypj.dev.model.Shoes;
import org.jypj.dev.model.ShoesImg;
import org.jypj.dev.service.ShoesService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenYu
 */
@Service("shoesService")
public class ShoesServiceImpl implements ShoesService {

    @Resource
    private ShoesDao shoesDao;

    @Resource
    private ShoesImgDao shoesImgDao;


    public List<ShoesImg> getShoesImgs(List<String> shoesImgList, String shoeId) {
        List<ShoesImg> shoesImgs = new ArrayList<>();
        for (String s : shoesImgList) {
            String shoesImgId = StringUtil.getLowUUID();
            ShoesImg shoesImg = new ShoesImg();
            shoesImg.setId(shoesImgId);
            shoesImg.setImageUrl(s);
            shoesImg.setShoesId(shoeId);
            shoesImg.setCreateTime(DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss"));
            shoesImgs.add(shoesImg);
        }
        return shoesImgs;
    }

    /**
     * 保存
     * 所有字段全都保存
     *
     * @param shoes
     * @return 保存后的对象包括ID
     */
    @Override
    public int saveShoes(Shoes shoes) {
        String shoeId = StringUtil.getLowUUID();
        shoes.setId(shoeId);
        shoes.setStatus("1");
        List<String> shoesImgList = shoes.getShoesImgs();
        List<ShoesImg> shoesImgs = getShoesImgs(shoesImgList, shoeId);
        shoesDao.saveShoes(shoes);
        shoesImgDao.saveShoesImgs(shoesImgs);
        return 0;
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    @Override
    public int deleteShoesById(String id) {
        shoesDao.deleteShoesById(id);
        shoesImgDao.deleteShoesImgByShoesId(id);
        return 0;
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param shoes
     * @return 保存后的对象包括ID
     */
    @Override
    public int updateShoes(Shoes shoes) {
        //将原来保存的删除掉
        shoesImgDao.deleteShoesImgByShoesId(shoes.getId());
        List<ShoesImg> shoesImgs = getShoesImgs(shoes.getShoesImgs(), shoes.getId());
        if (shoesImgs.size() > 0) {
            shoesImgDao.saveShoesImgs(shoesImgs);
        }
        shoesDao.updateShoes(shoes);
        return 0;
    }

    /**
     * 按ID查询
     *
     * @return Shoes
     * @parm id 主键ID
     */
    @Override
    public Shoes selectShoesById(String id) {

        return shoesDao.selectShoesById(id);
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param shoes 查询条件
     * @return Page
     */
    @Override
    public Page selectOnePageByShoes(PageVo pageVo, Shoes shoes) {
        return PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize()).doSelectPage(() -> shoesDao.selectOnePageByShoes(shoes));
    }
}